<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login
 *
 * @author cs321ga3a
 */
class settings extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }
	
	function cancelVote(){
		$url = kitchen_base_url()."cancelVote";
		$args = array(
			'user_id' => $this->session->userdata('user_id')
		);
		$result = $this->curl->simple_post($url, $args);
		if ($result == "fail"){
			echo "No vote found.";
			return;
		}
		echo $result;
	}
	
	function cancelOrder(){
		$url = kitchen_base_url()."cancelOrder";
		$args = array(
			'user_id' => $this->session->userdata('user_id')
		);
		$result = $this->curl->simple_post($url, $args);
		if ($result == "fail"){
			echo "No order found.";
			return;
		}
		echo $result;
	}
	

	function saveAllergies() {
	
		$allergies = $this->input->post('allergies');
		
		if (!is_array($allergies)){
			$allergies = array();
		}
		
		$url = kitchen_base_url()."saveAllergies";
		$args = array(
			'user_id' => $this->session->userdata('user_id'),
			'allergies' => $allergies,
		);
		
		$result = $this->curl->simple_post($url, $args);
		
		echo $result;
		
	
	}

	
	function manageAllergies() {
		
        $url = kitchen_base_url()."getAllergies";
		
		$args = array(
			'user_id' => $this->session->userdata('user_id'));
		$result = $this->curl->simple_post($url, $args);
        $xml = simplexml_load_string($result);
		$allergies = $xml->allergy;
		
		$data = array(      
            'title' => 'Allergy  Settings',
            'payload' => $allergies,
            'view' => 'view_allergies',
			'settings' => false,
			'back'	=> true,
			'allergies' => true,
        );
			
		$this->load->view('view_template', $data);   
	
    }

	function showSettings() {
		
		$data = array(      
            'title' => 'Settings',
            'payload' => "",
            'view' => 'view_settings',
			'settings' => false,
			'home'	=> true,
			'device' => $this->mobile_user_agent_switch(),
			'username' => $this->session->userdata('username')
        );
        
        
        $this->load->view('view_template', $data);   

	
	}
        
	function changePassword() {
        extract($this->ajax->post("oldpassword","newpassword1","newpassword2"));
		$this->load->model("model_user");
		if($newpassword1 != $newpassword2) {
			echo "New Passwords do not match";
			return;
		}

		$url = kitchen_base_url().'changePassword';
		$args = array(
			'user_id' => $this->session->userdata('user_id'),
			'oldpass' => encrypt_password($oldpassword),
			'newpass' => encrypt_password($newpassword1),
		);
		$result = $this->curl->simple_post($url, $args);
		echo $result;
	}
			

	function showChangePassword() {
	
		$data = array(      
            'title' => 'Change Password',
            'payload' => "",
            'view' => 'view_password',
			'settings' => false,
			'back'	=> true
        );
        
        
        $this->load->view('view_template', $data);   

	}
        
    function index() {
        
		$this->showSettings();

	}
	

	function mobile_user_agent_switch(){
		$device = '';
 
		if( stristr($this->input->user_agent(),'ipad') ) {
			$device = "iPad";
		} 
		else if( stristr($this->input->user_agent(),'iphone')) {
			$device = "iPhone";
		}
		else if( stristr($this->input->user_agent(),'ipod')) {
			$device = "iPod";
		} 
		else if( stristr($this->input->user_agent(),'android')) {
			$device = "Android";
		}

		if( $device ) {
			return $device; 
		} 
		return false;
	}	
        
    
}
?>
